/**
 * Created by zhangqingxi on 2017-04-06.
 */
var modelPath=ROOT_PATH+'/emergency/plan/';

//初始化部门
function initDept() {

    $('#deptId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}
//初始化 预案定义
function initDefinition() {
    $('#definitionId').combobox({
        url:dictionaryByCode+planDefinition,
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text'
    });
}
//初始化 预案层级 planHierarchy
function initHierarchy() {
    $('#hierarchyId').combobox({
        url:dictionaryByCode+planHierarchy,
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text'
    });
}
//初始化 预案分类
function initClassification() {
    $('#classificationId').combobox({
        url:dictionaryByCode+planClassification,
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text',
        onChange:function (newValue,oldValue) {
            $('#typeId').combobox({
                value:''
            });
           //根据Id 获得标识
            findClassificationCodeById(newValue)
        }
    });

}
/**
 * 根据Id查询code
 * @param id
 */
function findClassificationCodeById(id) {
    $.ajax({
        type: "POST",
        url: dictionaryById+id,
        dataType: "json",
        success: function(data){
            initType(data.code)
        }
    });
}
//初始化 预案类型
function initType(code) {

    $('#typeId').combobox({
        url:dictionaryByDatas,
        queryParams:{
            data1:code
        },
        prompt:'请选择',
        required:true,
        editable:false,
        multiple:true,
        valueField:'id',
        textField:'text'
    });
}
//初始化 情景模拟
function initScenarioConstruction() {
    $('#scenarioConstruction').combobox({
        data:[{id:0,text:'否'},{id:1,text:'是'}],
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text',
        panelHeight:'auto'
    });
}


function classificationLoadSuccess(){
    var $val= $('#classificationId').val();
    findClassificationCodeById($val);
    initClassification();
}

/**
 * 表单提交
 * @param url
 */
function initSaveButtonClick(url) {

    $('#btn_save').on('click',function () {

        var index=layer.confirm('是否确定保存？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $('#submit_form').form('submit', {
                url: url,
                onSubmit: function (param) {
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        layer.close(index);
                        layer.msg("请正确填写表单")
                    }
                    param['typeId']=$('#typeId').combobox('getValues').join(',');
                    return $(this).form('validate');
                },
                success: function (result) {
                    var data = $.parseJSON(result);
                    successCallback(data)
                }
            });
        });

    });
}
//初始化风险区域
function initDevice() {
    $("#deviceName").textbox({
        editable:false,
        required: true,
        iconWidth: 22,
        icons: [{
            iconCls: 'icon-add',
            handler: function (e) {
                add__device_func();
            }
        }, {
            iconCls: 'icon-remove',
            handler: function (e) {
                $(e.data.target).textbox('clear');
                $('#deviceId').val('');

            }
        }]
    });
}
function add__device_func(){
    WindowManager.openLayer({
        type: 2,
        title: '选择风险区域',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/risk/region/simpleList.shtml',
        callBack:function(data){//窗口关闭后的回调函数
            var id=data.id;
            var name=data.name;
            $('#deviceId').val(id);
            $('#deviceName').textbox('setValue', name);
        }
    });
}